﻿using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Prompts.Service.PromptService;

namespace Prompts.PromptServiceProxy
{
    [ServiceContract(ConfigurationName="Prompts.Prompting.Model.IPromptSelectionService")]
    public interface IPromptSelectionService
    {
        [OperationContractAttribute(
            AsyncPattern=true, 
            Action="http://tempuri.org/IPromptSelectionService/SetPromptSelections", 
            ReplyAction="http://tempuri.org/IPromptSelectionService/SetPromptSelectionsResponse")]
        IAsyncResult BeginSetPromptSelections(
            string path, 
            ObservableCollection<PromptSelectionInfo> promptSelections, 
            AsyncCallback callback, 
            object asyncState);

        SetPromptSelectionsResponse EndSetPromptSelections(IAsyncResult asyncResult);
    }
    
    public interface IPromptSelectionServiceChannel : IPromptSelectionService, IClientChannel
    {
    }

    public class PromptSelectionServiceClient 
        : ClientBase<IPromptSelectionService>, IPromptSelectionService2Client
    {
        public PromptSelectionServiceClient(Binding binding, EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }

        public void SetPromptSelectionsAsync(
            string path, 
            ObservableCollection<PromptSelectionInfo> promptSelections,
            Action<SetPromptSelectionsResponse> callBack)
        {

            InvokeAsync(
                (inValues, callback, asyncState) =>
                    {
                        var path1 = (string)inValues[0];
                        var promptSelections1 = (ObservableCollection<PromptSelectionInfo>)inValues[1];
                        return Channel.BeginSetPromptSelections(path1, promptSelections1, callback, asyncState);
                    }, 
                new object[] { path, promptSelections}, 
                result =>
                    {
                        try
                        {
                            var retVal = Channel.EndSetPromptSelections(result);
                            return new object[] { retVal };
                        }
                        catch (Exception e)
                        {
                            var response = new SetPromptSelectionsResponse
                                {
                                    ErrorOccured = true, 
                                    ErrorMessage = e.Message
                                };   
                            return new object[]{response};
                        }
                    }, 
                state =>
                    {
                        var args = (InvokeAsyncCompletedEventArgs)state;
                        callBack((SetPromptSelectionsResponse) args.Results[0]);
                    }, 
                new object());
        }
    }
}
